package kaaengine.loaders
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import kaaengine.display.IRenderable;
	import kaaengine.objects.IPose;
	import kaaengine.objects.isometric.IsoObject;
	import kaaengine.objects.isometric.poses.UniquePose;
	
	import mx.collections.ArrayCollection;
	
	[Event(name="OBJECTS_LOADED", type="flash.events.Event")]
	public class ObjectsLoader extends EventDispatcher
	{
		private static var DELIMITER:String = "\n";
		private var imagesDirectory:String;
		private var tab:Array;
		public var objectsRepository:ArrayCollection = new ArrayCollection();
		public var renderablesRepository:ArrayCollection;	
		public var posesRepository:ArrayCollection;
		
		public function ObjectsLoader(_imagesDirectory:String)
		{
			imagesDirectory = _imagesDirectory;
		}
		public function load(fileName:String):void {
			var request:URLRequest = new URLRequest(fileName);
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onObjectsFileLoaded);	
			loader.load(request);		
		}
		public function onObjectsFileLoaded(event:Event):void {
			var loader:URLLoader = URLLoader(event.target);
			var str:String = loader.data as String;
			tab = str.split(DELIMITER);
			var currentZone:String;
			
			renderablesRepository = new ArrayCollection();
			var imagesNamesTab:Array = new Array();
			
			
			for(var i:int=0;i< tab.length;i++){
				var line:String = tab[i];					
				if(line.substr(0,1)=="#") {
					currentZone = line;
				} else {	
					 if( currentZone == "#images"){							
							imagesNamesTab.push(line);
					}else {
						break;		
					}
				}
			}
			var il:ImageLoader = new ImageLoader();
			il.addEventListener("images_loaded",imagesLoaded);
			il.renderables = renderablesRepository;
			il.loadImagesFromArray(imagesDirectory,imagesNamesTab);
			
			
		}
		public function imagesLoaded(event:Event):void {
			trace("all objects images loaded");
			finish();
		}
		public function finish():void {
			var currentZone:String;
			objectsRepository = new ArrayCollection();
			posesRepository = new ArrayCollection();	
			for(var i:int=0;i< tab.length;i++){
				var line:String = tab[i];					
				if(line.substr(0,1)=="#") {
					currentZone = line;
				} else {	
					 if( currentZone == "#poses"){							
							var ss:Array = line.split(":");
							if( ss[0] == "U" ) {
								var p:IPose = new UniquePose();
								posesRepository.addItem(p);
								p.addRenderable(renderablesRepository.getItemAt(Number(ss[1])) as IRenderable);
							}
					} else if( currentZone == "#objects"){
						var ss:Array = line.split(",");
						var o:IsoObject = new IsoObject();
						o.xLength = Number(ss[1]);
						o.yLength = Number(ss[2]);
						o.addPose(posesRepository.getItemAt(Number(ss[3])) as IPose);
						o.x = Number(ss[4]);
						o.y = Number(ss[5]);
						o.name = ss[6];
						o.id = ss[0];
						objectsRepository.addItem(o);
					}
				}
			}
			trace("objects loaded");
			dispatchEvent(new Event("OBJECTS_LOADED"));		
		}

	}
}